﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.DataAccessHelper;
using DeviceInfoManage.DataAccessLayer;
/// <summary>
/// Department 的摘要说明
/// </summary>
public class MaintPerson
{
    private int MaintPersonId;           /*人员编号*/
    private string MaintPersonName;      /*人员姓名*/
    private string PhoneNumber;
    private string errMessage;          /*保存操作的错误信息*/

    public string ErrMessage
    {
        get { return errMessage; }
        set { errMessage = value; }
    }

    public MaintPerson()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /*根据人员编号得到人员名称*/
    public string GetMaintPersonName(int maintPersonId)
    {
        string quertString = "select maintPersonName from maintPersonInfo where maintPersonId = " + maintPersonId;
        DataSet ds = (new DataBase()).GetDataSet(quertString);
        return ds.Tables[0].Rows[0]["maintPersonName"].ToString();
    }
    /// <summary>
    /// 根据人员编号获取联系电话
    /// </summary>
    /// <param name="maintPersonId"></param>
    /// <returns></returns>
    public string GetPhoneById(int maintPersonId)
    {
        string quertString = "select PhoneNumber from maintPersonInfo where maintPersonId = " + maintPersonId;
        DataSet ds = (new DataBase()).GetDataSet(quertString);
        return ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
    }
    /*得到所有的部门信息*/
    public DataSet GetMaintPersonName()
    {
        string queryString = "select * from maintPersonInfo";
        DataBase db = new DataBase();
        return db.GetDataSet(queryString);
    }

    /*添加新的设备类别*/
    public bool AddMaintPersonName(string maintName, string pnumber)
    {
        /*首先判断系统中是否已经存在了该部门名称*/
        DataBase db = new DataBase();
        string queryString = "select * from maintPersonInfo where maintPersonName=" + SqlString.GetQuotedString(maintName);
        if (db.GetRecord(queryString))
        {
            this.errMessage = "该人员姓名系统已经存在!";
            return false;
        }
        /*如果系统中不存在该类别,就可以执行新的部门名称的加入*/
        string insertString = "insert into maintPersonInfo(maintPersonName,PhoneNumber) values(" + SqlString.GetQuotedString(maintName) + "," + SqlString.GetQuotedString(pnumber) + ")";
        if (db.InsertOrUpdate(insertString) < 0)
        {
            this.errMessage = "新人员信息加入数据库时发生了错误！";
            return false;
        }
        return true;
    }

    /*修改已经存在的部门*/
    public bool UpdateMaintPersonName(int maintId, string maintName)
    {
        /*首先判断系统中是否已经存在了该部门名称*/
        DataBase db = new DataBase();
        string queryString = "select * from maintPersonInfo where maintPersonName =" + SqlString.GetQuotedString(maintName);
        if (db.GetRecord(queryString))
        {
            this.errMessage = "该部门名称系统已经存在!";
            return false;
        }
        /*然后执行根据id更新部门名称信息*/
        string updateString = "update maintPersonInfo set maintPersonName =" + SqlString.GetQuotedString(maintName);
        updateString += " where maintPersonId=" + maintId.ToString();
        if (db.InsertOrUpdate(updateString) < 0)
        {
            this.errMessage = "修改部门名称时发生了错误！";
            return false;
        }
        return true;
    }

    /*删除某个分类编号的设备分类信息*/
    public bool DeleteMaintPersonName(int maintId)
    {
        /*首先判断该分类下是否还有设备，如果有是不能执行删除的*/
        DataBase db = new DataBase();
        string queryString = "select * from deviceInfo where maintPersonName=" + maintId;
        //if (db.GetRecord(queryString))
        //{
        //    this.errMessage = "该人员下还存在设备信息，不能执行删除操作!";
        //    return false;
        //}

        string deleteString = "delete from maintPersonInfo where maintPersonId=" + maintId;
        if (db.InsertOrUpdate(deleteString) < 0)
        {
            this.errMessage = "删除人员信息时发生了错误!";
            return false;
        }
        return true;
    }
}
